<?php
/**
 * Created by PhpStorm.
 * User: wangliang
 * Date: 2018/9/26
 * Time: 下午8:03
 */

namespace app\base\controller;
use think\Controller;
use think\facade\Config;

abstract class base extends controller
{
    static public function showReturnCode($code = '', $data = [], $msg = '')
    {
        $return_data = [
            'code' => '500',
            'msg' => '未定义消息',
            'data' => $code == 1001 ? $data : [],
        ];
        if (empty($code)) return $return_data;
        $return_data['code'] = $code;
        if(!empty($msg)){
            $return_data['msg'] = $msg;
        }else if (isset(ReturnCode::$return_code[$code]) ) {
            $return_data['msg'] = ReturnCode::$return_code[$code];
        }
        return $return_data;
    }
    /**
     * 获取返回码数组别名函数 以json格式返回
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param string $code
     * @param array $data
     * @param string $msg
     * @return array
     */
    static public function showJsonReturnCode($code = '', $data = [], $msg = ''){
        return json(self::showReturnCode($code, $data, $msg));
    }
    /**
     * 获取返回码数组别名函数 无返回值
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param string $code
     * @param string $msg
     * @return array
     */
    static public function showJsonReturnCodeWithOutData($code = '', $msg = ''){
        Config::set(['default_return_type'    => 'json',]);
        return self::showReturnCode($code, [], $msg);
    }

    /**
     * 获取返回码数组别名函数 无返回值
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param string $code
     * @param string $msg
     * @return array
     */
    static public function showReturnCodeWithOutData($code = '', $msg = '')
    {
        return self::showReturnCode($code,[],$msg);
    }

    /**
     * 获取当前类名称
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param bool|false $all
     * @return string
     */
    public function getClassName($all = false){
        return $all
            ? get_called_class()
            : basename(str_replace('\\', '/', get_called_class()),'.php');
    }

    /**
     * 数据库字段 网页字段转换
     * 标识为数据库字段 值为浏览器提交字段
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param $array   标识为数据库字段 值为浏览器提交字段
     * @param bool|false $uuid  是否追加UUID信息
     * @return array
     */
    protected function buildParam($array,$uuid=false)
    {
        $data=[];
        foreach( $array as $item=>$value ){
            $data[$item] = $this->request->param($value);
        }
        if ($uuid && isset($this->uuid)){
            $data['uuid'] = $this->uuid;
        }
        return $data;
    }

    /**
     * 快速修改
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param bool|false $parameter
     * @param bool|false $validate_name
     * @param bool|false $model_name
     * @param array $save_data
     * @return array
     */
    protected function editData($parameter = false, $validate_name = false, $model_name = false, $save_data = [])
    {
        if (empty($save_data)) {
            if ($parameter != false && is_array($parameter)) {
                $data = $this->buildParam($parameter);
            } else {
                $data = $this->request->post();
            }
        } else {
            $data = $save_data;
        }
        if (!$data) return $this->showReturnCode(1004);
        //追加登录UUID
        if ($this->checkLoginGlobal() && !isset($data['uuid'])) $data['uuid'] = $this->uuid;

        if ($validate_name != false) {
            $result = $this->validate($data, $validate_name);
            if (true !== $result) return $this->showReturnCodeWithOutData(1003,$result);
        }
        $model_edit = Loader::model($model_name);
        if (!$model_edit) return ['code' => 1010];
        return $model_edit->editData($data);
    }

    /**
     * 快速调用执行model函数
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param $param_data
     * @param bool|false $validate_name
     * @param bool|false $model_name
     * @param string $action_name
     * @return array
     */
    protected function doModelAction($param_data,$validate_name = false, $model_name = false,$action_name='editData'){

        if ($validate_name != false) {
            $result = $this->validate($param_data, $validate_name);
            if (true !== $result) return $this->showReturnCode(1003, [], $result);
        }
        $model_edit = Loader::model($model_name);
        if (!$model_edit) return $this->showReturnCode(1010);
        return $model_edit->$action_name($param_data);
    }

}